const chatService = require('./../services/chat.service');

module.exports = {
    /**
    * 聊天对象id获取
    */
   async getSenders(ctx) {
       let result = {
           result: false,
           message: '',
           new_sender: [],
           old_sender: []
       };
       let session = ctx.session;
       if (session.isLogin) {
           let message = 0;
           let user = ctx.query.user_id;
           let res_new = await chatService.getNewSender({user});
           if (res_new.length > 0) {
               let ids = [];
               for (let item of res_new) {
                   ids.push(item.sender);
               }
               result.result = true;
               result.new_sender = ids;
               message = 1;
           } 
           let res_old = await chatService.getOldSender({user});
           if (res_old.length > 0) {
               let ids = [];
               for (let item of res_old) {
                   ids.push(item.sender);
               }
               result.result = true;
               result.old_sender = ids;
               message = 1;
           } 

           if (message==0) {
               result.message = '暂无聊天对象';
           }
           result.result = true;
       }else {
           result.message = '请先登录';
       }
       ctx.body = result;
   },
    /**
     * 聊天室id获取
     */
    async getRoom(ctx) {
        let result = {
            result: false,
            message: '',
            chat_room: ""
        };
        let session = ctx.session;
        if (session.isLogin) {
            let user1 = ctx.query.user1_id;
            let user2 = ctx.query.user2_id;
            let res = await chatService.getRoom({
                user1,
                user2
            });
            if (res) {}
            else{
                res = await chatService.addRoom({
                    user1,
                    user2
                });
            }
            result.result = true;
            result.chat_room = res.room_id; 
        }else {
            result.message = '请先登录';
        }
        ctx.body = result;
    },
    /**
     * 聊天记录添加
     */
    async addMessage(ctx) {
        let result = {
            result: false,
            message: ''
        };
        let formData = ctx.request.body;
        let session = ctx.session;
        if (session.isLogin) {
            let res = await chatService.addMessage({
                ...formData
            });
            if (res && res.affectedRows) {
                result.result = true;
                result.message = '聊天记录添加成功';
            }
        } else {
            result.message = '请先登录';
        }
        ctx.body = result;
    },
    /**
     * 聊天记录获取
     * 
     */
    async getMessage(ctx) {
        let result = {
            result: false,
            message: '',
            chat_read: [],
            chat_unread: []
        };
        let session = ctx.session;
        if (session.isLogin) { 
            let room = ctx.query.chatroom_id;
            result.room = room;
            result.user = session.user_id;
            let res_read = await chatService.getReadMessage({ room,user_id:session.user_id });
            let res_unread = await chatService.getUnReadMessage({ room,user_id:session.user_id });
            result.result = true;
            result.chat_read = res_read;
            result.chat_unread = res_unread;
        }else {
            result.message = '请先登录';
        }
        ctx.body = result;
    },
    /**
     * 聊天记录修改
     */
    async modifyMessageStates(ctx) {
        let result = {
            result: false,
            message: '暂无修改'
        };
        let user_id = ctx.session.user_id || false;
        let formData = ctx.request.body;
        if (user_id) {
            let res = await chatService.modifyMessageStates({
                room: formData.chatroom_id,
                receiver: user_id
            });
            if (res && res.affectedRows) {
                result.result = true;
                result.message = '聊天记录状态修改成功';
            }
        } else {
            result.message = '请先登录';
        }
        ctx.body = result;
    }
}